<template>
  <el-form
      :model="twoPwd"
      ref="twoPwd"
      :rules="rules">
    <el-form-item label="旧密码" prop="oldPassword">
      <el-input v-model="twoPwd.oldPassword" placeholder="请输入"
                style="position: relative ;left: 67px ; width:500px "></el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="newPassword">
      <el-input v-model="twoPwd.newPassword" placeholder="请输入"
                style="position: relative ;left: 67px ; width:500px "></el-input>
    </el-form-item>
    <el-form-item label="再次输入新密码" prop="ReNewPassword">
      <el-input v-model="twoPwd.ReNewPassword" placeholder="请输入"
                style="position: relative ;left: 13px ; width:500px "></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="revisePwd('twoPwd')">提交</el-button>
      <el-button @click="ChangePassword= false,goBack">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import request from "@/util/request.js";
import {ElMessage} from "element-plus";
export default {
  data() {
    return {
      ChangePassword: false,
      UploadInformation: false,
      information: {},
      //新旧密码和学号
      twoPwd:{
        accountNo: "",
        oldPassword: "",
        newPassword: "",
        ReNewPassword:"",
      },
      rules: {
        oldPassword: [
          { required: true, message: '请输入旧密码', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        ReNewPassword:[
          { required: true, message: '请再输入新密码', trigger: 'blur' }
        ]
      },
    }
  },
  created() {
    this.AccountNo();
  },
  methods: {
    revisePwd(pwd){
      this.$refs[pwd].validate((valid) => {
        if(valid){
          if (this.twoPwd.newPassword !== this.twoPwd.ReNewPassword) {
            ElMessage.error("两次新密码不一致")
            return
          }
          request.post("/account/revisePwd", this.twoPwd).then(res => {
            if (res.data.code === '400') {
              ElMessage.error("密码错误")
            } else if (res.data.code === '200') {
              ElMessage.success("修改成功")
              this.ChangePassword= false
              this.goBack()
              this.$router.push("/login")
            }
          })
        }else {
          return false;
        }
      })
    },
    AccountNo(){
      this.twoPwd.accountNo = JSON.parse(localStorage.getItem('user')).accountNo;
    },
    goBack(){
      this.twoPwd.newPassword = ""
      this.twoPwd.oldPassword = ""
      this.twoPwd.ReNewPassword = ""
    },
  }

}

</script>

<style scoped>

</style>
